package la.iok.hzsvn.lewin.movie.controller;

import la.iok.hzsvn.lewin.movie.core.Token;
import la.iok.hzsvn.lewin.movie.model.AppPackageItemParam;
import la.iok.hzsvn.lewin.movie.service.AppPackageItemService;
import la.iok.hzsvn.share.response.OperatorResponse;
import la.iok.hzsvn.share.response.OperatorResponses;
import la.iok.hzsvn.share.response.SingleDataResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("appPackageItems")
public class AppPackageItemController {
    private final AppPackageItemService appPackageItemService;

    public AppPackageItemController(AppPackageItemService appPackageItemService) {
        this.appPackageItemService = appPackageItemService;
    }
    @PostMapping
    public SingleDataResponse<Long> save(@RequestHeader Token token, @RequestBody AppPackageItemParam param){
        return new SingleDataResponse<>(appPackageItemService.save(token,param));
    }
    @GetMapping("{id}/exists")
    public SingleDataResponse<Boolean> exist(@PathVariable Long id){
        return new SingleDataResponse<>(appPackageItemService.exist(id));
    }
    /**
     * 上传剧幕文件
     * @param token 操作用户
     * @param id 剧幕id
     * @param file 封面文件
     * @return 操作结果
     */
    @PostMapping("{id}/file")
    public OperatorResponse uploadFile(@RequestHeader Token token, @PathVariable Long id, MultipartFile file){
        appPackageItemService.uploadFile(token,id,file);
        return OperatorResponses.success();
    }

    @GetMapping("{id}/file")
    public void downloadFile(HttpServletResponse response, @PathVariable Long id){
        appPackageItemService.downloadFile(response,id);
    }

    @GetMapping("{id}/digest")
    public SingleDataResponse<String> digest(@PathVariable Long id){
        String digest = appPackageItemService.digest(id);
        SingleDataResponse<String> res = new SingleDataResponse<>();
        res.setData(digest);
        return res;
    }

    @GetMapping("{id}/storePath")
    public SingleDataResponse<String> storePath(@PathVariable Long id){
        String storePath = appPackageItemService.storePath(id);
        return OperatorResponses.stringSingleDataResponse(storePath);
    }
}
